//
//  CJTableViewModel.m
//  ChangJia
//
//  Created by 张育铭 on 2017/1/11.
//  Copyright © 2017年 Zhangyuming. All rights reserved.
//

#import "CJTableViewModel.h"

@implementation CJTableViewModel
- (instancetype)initHotWithDict:(NSDictionary*)dict {
    _theTitleItem       = [NSString stringWithFormat:@" %@",dict[@"title"]];
    _theId              = [dict[@"id"] stringValue];
    return self;
}

- (instancetype)initWithDict:(NSDictionary*)dict {
    NSLog(@"%@",dict);
    _thePeoplePhotoItem = [self theImage50Path:dict[@"user"][@"avatar"]];
    _thePeopleNameItem  = dict[@"user"][@"name"];
    _theTimeLabelItem   = [self turningMillisecondsForDate:[dict[@"created_at"] longLongValue]];
    _theTitleItem       = dict[@"title"];
    _theLikecount       = [dict[@"reply_count"] stringValue];
    _theIsType          = [dict[@"type"] stringValue];
    _theId              = [dict[@"id"] stringValue];
    if ([_theIsType isEqualToString:@"2"]) {
        NSString *theString = [dict[@"attach"][@"src_type"]stringValue];
        if ([theString isEqualToString:@"1"]) {
               _theContenLabelItem = dict[@"attach"][@"origin"][@"content"][@"content"];
            if ([dict[@"attach"][@"origin"][@"content"][@"images"] count] > 0) {
                NSLog(@"%@",dict[@"attach"][@"origin"][@"content"][@"images"][0][@"url"]);
                NSLog(@"%@",dict[@"attach"][@"origin"][@"content"][@"images"][0][@"url"]);
                _theInterlinkageImg = [self theImage150Path:dict[@"attach"][@"origin"][@"content"][@"images"][0][@"url"]];
            }
           }
        else if ([theString isEqualToString:@"2"]) {
            _theContenLabelItem = dict[@"attach"][@"origin"][@"title"];
            _theInterlinkageImg = @"";
        }
        else if ([theString isEqualToString:@"4"]) {
            _theContenLabelItem = [NSString stringWithFormat:@"%@的个人中心",dict[@"attach"][@"origin"][@"name"]];
            _theInterlinkageImg = [self theImage150Path:dict[@"attach"][@"origin"][@"avatar"]];
        }
        else if ([theString isEqualToString:@"5"]) {
            _theContenLabelItem = [NSString stringWithFormat:@"分享%@",dict[@"content"]];
            _theInterlinkageImg = @"";
        }
        else if ([theString isEqualToString:@"9"]) {
            _theContenLabelItem = dict[@"attach"][@"origin"][@"title"];
            _theInterlinkageImg = [self theImage150Path:dict[@"attach"][@"origin"][@"images"][0]];
        }
        else {
            /// 8  10  11
            _theContenLabelItem = dict[@"attach"][@"origin"][@"title"];
            _theInterlinkageImg = [self theImage150Path:dict[@"attach"][@"origin"][@"cover"]];
        }
    }
    else {
        _thePhotoArray  = [NSMutableArray array];
        NSString *str = dict[@"images"];
        if ([str isKindOfClass:[NSNull class]] || [str isEqual:[NSNull null]] || str == nil) {
            NSLog(@"1111");
        } else {
            [dict[@"images"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [_thePhotoArray addObject:[self theImage150Path:obj]];
            }];
            NSLog(@"%@",_thePhotoArray);
         }
        _theContenLabelItem = dict[@"content"];
       }
    return self;
}



- (instancetype)initReplaceWithDict:(NSDictionary*)dict {
    NSLog(@"%@",dict);
    _thePeoplePhotoItem = [self theImage50Path:dict[@"user"][@"avatar"]];
    _thePeopleNameItem  = dict[@"user"][@"name"];
    _theTimeLabelItem   = [self turningMillisecondsForDate:[dict[@"created_at"] longLongValue]];
    if ([dict[@"content"] isKindOfClass:[NSNull class]] || [dict[@"content"] isEqual:[NSNull null]] || dict[@"content"] == nil) {
        _theContenLabelItem = @"";
    } else {
       _theContenLabelItem = dict[@"content"];
    }
    return self;
}


- (NSString*)theImage50Path:(NSString*)url {
    NSString *urlStr = [NSString stringWithFormat:@"%@?imageView2/0/h/50",url];
    return urlStr;
}
- (NSString*)theImage150Path:(NSString*)url {
    NSString *urlStr = [NSString stringWithFormat:@"%@?imageView2/0/h/150",url];
    return urlStr;
}


/**************************************************************/
//将毫秒数转格式化（刚刚、x分钟前）
- (NSString *)turningMillisecondsForDate:(long long)second
{
    //转化成NSDate
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:second];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSInteger unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday;
    NSDateComponents * component=[calendar components:unitFlags fromDate:date];
    
    NSUInteger year = [component year];
    NSUInteger month = [component month];
    NSUInteger day = [component day];
    
    NSUInteger hour = [component hour];
    NSUInteger minute = [component minute];
    
    NSDate * today = [NSDate date];
    component = [calendar components:unitFlags fromDate:today];
    
    NSUInteger t_year = [component year];
    
    NSString *string = nil;
    
    long long now = [today timeIntervalSince1970];
    
    long distance = now - second;
    
    if(distance < 60)
    {
        string = @"刚刚";
    }
    else if(distance < 60*60)
    {
        string = [NSString stringWithFormat:@"%ld分钟前", distance/60];
    }
    else if(distance < 60*60*24)
    {
        string = [NSString stringWithFormat:@"%ld小时前", distance/60/60];
    }
    else if(distance < 60*60*24*7)
    {
        string = [NSString stringWithFormat:@"%ld天前", distance/60/60/24];
    }
    else if(year == t_year)
    {
        string=[NSString stringWithFormat:@"%02lu-%02lu %lu:%02lu", (unsigned long)month, (unsigned long)day, (unsigned long)hour, (unsigned long)minute];
    }
    else
    {
        string=[NSString stringWithFormat:@"%lu-%lu-%lu", (unsigned long)year, (unsigned long)month, (unsigned long)day];
    }
    
    return string;
    
}


@end
